package persentation.presentStudent;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import businesslogic.userBl.StudentController;
import dataservice.ResultMessage;

public class ChangePasswordPanel extends JPanel {
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	private JPasswordField passwordField_2;
	private StudentController studentController;
	private static Image INFO=new ImageIcon("Graphics/studentGraphics/info.png").getImage();
	

	/**
	 * Create the panel.
	 */
	public ChangePasswordPanel(StudentController studentController) {
		setSize(600, 600);
		setLayout(null);
		this.studentController=studentController;
		JLabel lblNewLabel = new JLabel("输入新密码");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		lblNewLabel.setBounds(250, 105, 170, 34);
		add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("确认新密码");
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		lblNewLabel_1.setBounds(250, 149, 155, 49);
		add(lblNewLabel_1);
		
		JButton button = new JButton("确认");
		button.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i=JOptionPane.showConfirmDialog(null,"确认修改密码？","确认",JOptionPane.YES_NO_OPTION);
				if(i==JOptionPane.YES_OPTION){
					if (!isSamePassword(passwordField_1.getPassword(),passwordField_2.getPassword())) {
						JOptionPane.showMessageDialog(null,"两次输入密码不同","失败！",JOptionPane.ERROR_MESSAGE);
					}
					else {
						if (changepassword()) {
							JOptionPane.showMessageDialog(null,"修改密码成功！","成功！",JOptionPane.INFORMATION_MESSAGE);
						}
						else {
							JOptionPane.showMessageDialog(null,"原密码错误！","失败！",JOptionPane.ERROR_MESSAGE);
						}
					}
					passwordField.setText(null);
					passwordField_1.setText(null);
					passwordField_2.setText(null);
				}
			
				
			}
		});
		button.setBounds(250, 279, 93, 23);
		add(button);
		
		JButton button_1 = new JButton("重置");
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				passwordField_1.setText(null);
				passwordField_2.setText(null);
			}
		});
		button_1.setBounds(450, 279, 93, 23);
		add(button_1);
		
		JLabel label = new JLabel("原密码");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label.setBounds(250, 52, 104, 55);
		add(label);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(430, 71, 160, 21);
		add(passwordField);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(430, 114, 160, 21);
		add(passwordField_1);
		
		passwordField_2 = new JPasswordField();
		passwordField_2.setBounds(430, 165, 160, 21);
		add(passwordField_2);
	}
	
	private boolean isSamePassword(char[] first,char[] second){
		if (first.length!=second.length) {
			return false;
		}
		else {
			for (int i = 0; i < second.length; i++) {
				if (first[i]!=second[i]) {
					return false;
				}
			}
			return true;
		}
	}
	
	private boolean changepassword(){
		ResultMessage resultMessage=studentController.changePassword(passwordField.getPassword()
				, passwordField_1.getPassword(), passwordField_2.getPassword());

		if (resultMessage==ResultMessage.MODIFY_SUCCESS) {
			return true;
		}
		return false;
	}
	
	public void paintComponent(Graphics g){
		g.drawImage(INFO, 0,0, null);
	}
}
